package org.example.review.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Logger;

@Component
@EnableConfigurationProperties
public class DataSourceTest implements CommandLineRunner {

    private static final Logger logger = Logger.getLogger(DataSourceTest.class.getName());

    @Autowired
    private DataSource dataSource;

    @Override
    public void run(String... args) throws Exception {
        logger.info("开始测试数据源配置...");

        if (dataSource == null) {
            logger.severe("数据源为空！请检查配置");
            return;
        }

        logger.info("数据源类型: " + dataSource.getClass().getName());

        try (Connection connection = dataSource.getConnection()) {
            logger.info("数据库连接成功！");
            logger.info("数据库URL: " + connection.getMetaData().getURL());
            logger.info("数据库名称: " + connection.getMetaData().getDatabaseProductName());
            logger.info("数据库版本: " + connection.getMetaData().getDatabaseProductVersion());
        } catch (SQLException e) {
            logger.severe("数据库连接失败: " + e.getMessage());
        }
    }
}